/**
 *
 */
package com.shengdou.as.slopsapptest.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shengdou.as.slopsapptest.R;

/**
 * @author Administrator
 */
public class LoadingDialog {
    private Context context;
    private Dialog LoadingDialog;
    private TextView tipTextView;

//    ShimmerFrameLayout shimmerFrameLayout;

    public LoadingDialog(Context context) {
        super();
        this.context = context;
    }

    /**
     * 得到自定义的progressDialog
     *
     * @param @context
     * @param @msg
     * @return
     */
    public Dialog builder() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.loading_dialog, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
//        shimmerFrameLayout = (ShimmerFrameLayout) v.findViewById(R.id.shimmerContent);
//        shimmerFrameLayout.setIntensity(0.5f); //设置光的强度，根据测试来看，应该是被光照的边缘部分的透明度。
//        shimmerFrameLayout.setRepeatMode(ObjectAnimator.REVERSE);// 设置shimmer动画重复类型   REVERSE表示，闪光从左到右，之后在从右到左这样往复；  RESTART表示闪光每次总是从左到右。
//        shimmerFrameLayout.setAngle(ShimmerFrameLayout.MaskAngle.CW_0); //设置shimmer闪光的倾斜角度   MaskAngle.CW_0 表示从左到右的方向  MaskAngle.CW_90 表示从上到下的方向  MaskAngle.CW_180 表示从右到左的方向 MaskAngle.CW_270 表示从下到上的方向
//        shimmerFrameLayout.setDropoff(0.5f);   //设置shimmer闪光的宽度  该值表示的是一个相对的宽度，即表示整个ShimmerFrameLayout宽度的比例。即若设置该值为0.5f则表示光的宽度是ShimmerFrameLayout的一半。
//        shimmerFrameLayout.setBaseAlpha(0.5f); //设置没有光照的地方的透明度。
//        shimmerFrameLayout.setMaskShape(ShimmerFrameLayout.MaskShape.LINEAR); //设置光的形状      //圆形或者叫辐射状ShimmerFrameLayout.MaskShape.RADIAL
//        shimmerFrameLayout.startShimmerAnimation();//设置字体闪光动画

        // main.xml中的ImageView
        ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img);
        ImageView loading = (ImageView) v.findViewById(R.id.loading);
        tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字
        // 加载动画
        Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(
                context, R.anim.load_animation);
        // 使用ImageView显示动画
        spaceshipImage.startAnimation(hyperspaceJumpAnimation);
//        loading.setImageResource(R.drawable.loading);
//        AnimationDrawable animationDrawable = (AnimationDrawable) loading.getDrawable();
//        animationDrawable.start();

        // LoadingDialog = new Dialog(context, R.style.loading_dialog);//
        // 创建自定义样式dialog
        LoadingDialog = new Dialog(context, R.style.loading_dialog);// 创建自定义样式dialog

        LoadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT));// 设置布局

        return LoadingDialog;

    }

    // 是否可以用“返回键”取消
    public void setCancelable(boolean cancel) {
        LoadingDialog.setCancelable(cancel);
    }

    //Dialog是否显示在前台
    public boolean isShowing() {
        boolean showing = LoadingDialog.isShowing();
        return showing;
    }

    /**
     * 只有按返回键能取消
     *
     * @param cancel
     * @return
     */
    public Dialog setBACKCancelable(final boolean cancel) {
        LoadingDialog.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    if (cancel) {
                        dialog.dismiss();
//                        shimmerFrameLayout.stopShimmerAnimation();
                    }
                }
                return false;
            }
        });
        return LoadingDialog;
    }

    public void setdismiss() {
        try {
            if (LoadingDialog != null && LoadingDialog.isShowing()) {
                LoadingDialog.dismiss();
//                shimmerFrameLayout.stopShimmerAnimation();

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public Dialog setMsg(String msg) {
        if ("".equals(msg)) {
            tipTextView.setText("加载中...");
        } else {
            tipTextView.setText(msg);
        }
        return LoadingDialog;
    }

    public void show() {
        try {
            if (LoadingDialog != null && !LoadingDialog.isShowing()) {
                LoadingDialog.show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
